'''
Created on 2010-4-7

@author: chengmeng
'''

import ogre.renderer.OGRE as ogre 
import wx 


class CameraPlugin:

	def __init__(self,frame): 
		self.sceneManager = frame.plugins.OgreWindowPlugin.sceneManager
		self.renderWindow = frame.plugins.OgreWindowPlugin.renderWindow
		
		self.createCamera()
		self.createViewport()

		frame.plugins.OgreWindowPlugin.addMouseListener(self._MouseEventListener)
		frame.plugins.OgreWindowPlugin.addKeyDownListener(self._KeyDownEventListener)
		

		d=50.0 #displacement for key strokes 
		self.ControlKeyDict={wx.WXK_LEFT:ogre.Vector3(-d,0.0,0.0), 
							 wx.WXK_RIGHT:ogre.Vector3(d,0.0,0.0), 
							 wx.WXK_UP:ogre.Vector3(0.0,0.0,-d), 
							 wx.WXK_DOWN:ogre.Vector3(0.0,0.0,d), 
							 wx.WXK_PAGEUP:ogre.Vector3(0.0,d,0.0), 
							 wx.WXK_PAGEDOWN:ogre.Vector3(0.0,-d,0.0)} 
		pass

	def getCamera(self): 
		return self.Camera

	def getCameraNode(self): 
		return self.cameraNode

	def createCamera(self):
		"create a Camera" 
		if self.sceneManager.hasCamera('#EditCamera'):
			self.Camera = self.sceneManager.getCamera('#EditCamera')
		else:
			self.Camera = self.sceneManager.createCamera('#EditCamera') 
			self.Camera.lookAt(ogre.Vector3(0, 0, 0))
			self.Camera.nearClipDistance = 5
			self.Camera.setAutoAspectRatio( True )

		import environment
		self.Camera.setQueryFlags(environment.QueryFlags)

		# create the camera nodes & attach camera 
		if self.sceneManager.hasSceneNode('#cameraNode'):
			self.cameraNode = self.sceneManager.getSceneNode("#cameraNode") 
			self.PitchNode = self.sceneManager.getSceneNode("#PitchNode") 
		else:
			self.cameraNode = self.sceneManager.getRootSceneNode().createChildSceneNode("#cameraNode", ogre.Vector3(0, 0, 250)) 
			self.PitchNode = self.cameraNode.createChildSceneNode("#PitchNode") 
			self.PitchNode.attachObject(self.Camera)

		pass

	def createViewport(self):	
		"create a Viewport" 
		renderWindow = self.renderWindow 
		renderWindow.removeAllViewports()
		viewport = renderWindow.addViewport(self.Camera, 0, 0.0, 0.0, 1.0, 1.0) 
		viewport.backgroundColour = ogre.ColourValue(0, 0, 0) 
		pass

	def _KeyDownEventListener(self,event): 

		##
		validMove = self.ControlKeyDict.get(event.m_keyCode,False) 
		if validMove : 
			self.cameraNode.translate(validMove,self.cameraNode.TS_LOCAL) 
			event.Skip() 
		pass


	def _MouseEventListener(self, event): 

		dx = 0
		dy = 0
		f = -0.05
		
		##
		if event.RightDown(): #Precedes dragging 
			self.StartDragX, self.StartDragY = event.GetPosition() #saves position of initial click 
			event.Skip()
		elif event.Dragging() and event.RightIsDown():
			x,y = event.GetPosition() 
			dx = (x-self.StartDragX) * f
			dy = (y-self.StartDragY) * f
			self.StartDragX, self.StartDragY = x, y 
			event.Skip()


		if event.AltDown():
			
			self.cameraNode.translate(ogre.Vector3(dx,-dy,0.0),self.cameraNode.TS_LOCAL) 

		elif event.ControlDown():
			
			self.cameraNode.translate(ogre.Vector3(dx,0.0,dy),self.cameraNode.TS_LOCAL) 

		elif event.RightIsDown():
	
			self.cameraNode.yaw(ogre.Degree(dx*2.0)) 
			self.PitchNode.pitch(ogre.Degree(dy*2.0)) 


		pass

	def IBeginLoadScene(self):
		self.Camera = None
		self.cameraNode = None
		pass
	
	def IEndLoadScene(self):
		self.createCamera()
		self.createViewport()
		pass

	def IClearScene(self):
		self.createCamera()
		self.createViewport()
		pass


##
def IInit(frame):
	return CameraPlugin(frame)




